//
//  SISSInfomationViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/2.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSInfomationViewController.h"
#import "SISSOrderEvaluateViewCell.h"
#import "SISSStarsShowView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SISSNetworkingManager+Comment.h"
#import "SISSReceivedCommentViewController.h"

static NSString *orderEvaluateViewCellReuseID = @"OrderEvaluateViewCellReuseID";
@interface SISSInfomationViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *topIconNameView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet SISSStarsShowView *starsView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeightConstraint;
@property (weak, nonatomic) IBOutlet UILabel *houseResourceNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *passengerCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *rentOutCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *tenantCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UILabel *commentsCount;
@property (weak, nonatomic) IBOutlet UILabel *starServiceNoLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyLabel;
@property (weak, nonatomic) IBOutlet UILabel *storefrontLabel;
@property (weak, nonatomic) IBOutlet UILabel *storeAddressLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *businessContactViewHeightConstraint;
@property (weak, nonatomic) IBOutlet UIImageView *topBgImageView;

/** 记录选中按钮状态 */
@property (weak, nonatomic) UIButton *selectedButton;
@property (weak, nonatomic) IBOutlet UIButton *houseCommentButton;
@property (weak, nonatomic) IBOutlet UIButton *checkAllButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *consultView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightConstraint;

/* 评论数组 */
@property (strong, nonatomic) NSArray<SISSCommentModel *> *commentArr;

@end

@implementation SISSInfomationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectedButton = self.houseCommentButton;
    self.starsView.isInfo = YES;
    self.title = @"姓名";
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.topHeightConstraint.constant = kTopHeight; // 适配iPhone X
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backItemAction)];
}

- (void)backItemAction {
    
//    self.navigationController.navigationBar.backgroundColor = [UIColor de];
    //    如果不想让其他页面的导航栏变为透明 需要重置
    [self.navigationController.navigationBar setBackgroundImage:[ImageWithColor imageWithColor:[UIColor colorWithWhite:1 alpha:0.99]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[ImageWithColor imageWithColor:[UIColor colorWithWhite:1 alpha:0.99]]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                      NSForegroundColorAttributeName:COLOR(0x334A60),NSFontAttributeName:[UIFont systemFontOfSize:17]
                                                                      }];
    
    
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
     
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                    NSForegroundColorAttributeName:COLOR(0xFFFFFF),NSFontAttributeName:[UIFont systemFontOfSize:17]
                                                                      }];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
}

- (void)setSpId:(NSString *)spId {
    _spId = spId;
    [self requestTakeLookCommentData];

}

- (void)setPersonInfo:(SISSPersonInfoModel *)personInfo {
    
    [self.view layoutIfNeeded];
    _personInfo = personInfo;
    
    self.title = personInfo.name;
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:personInfo.portrait] placeholderImage:[UIImage imageNamed:@"头像"]];
    self.starsView.stars = personInfo.star_level;
    self.houseResourceNumLabel.text = [NSString stringWithFormat:@"%zd",personInfo.houseCount];
    self.passengerCountLabel.text = [NSString stringWithFormat:@"%zd",personInfo.demandCount];
    self.rentOutCountLabel.text = [NSString stringWithFormat:@"%zd",personInfo.leaseCount];
    self.tenantCountLabel.text = [NSString stringWithFormat:@"%zd",personInfo.rentCount];
    self.scoreLabel.text = personInfo.score.length > 0 ? personInfo.score : @"0";
    self.commentsCount.text = [NSString stringWithFormat:@"共%zd人评价",personInfo.comment_count];
    
    self.starServiceNoLabel.text = personInfo.license_no ? personInfo.license_no : @" ";
    self.companyLabel.text = personInfo.organName ? personInfo.organName : @" ";
    self.storefrontLabel.text = personInfo.organBranchName ? personInfo.organBranchName : @" ";
    NSString *address = [personInfo.organBranchName isEqualToString:@"总部"] ? personInfo.office_address : personInfo.office_address;
    self.storeAddressLabel.text = address ? address : @" ";
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    self.checkAllButton.hidden = self.commentArr.count < 2 ? YES : NO;
    return self.commentArr.count > 2 ? 2 : self.commentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSOrderEvaluateViewCell *cell = [tableView dequeueReusableCellWithIdentifier:orderEvaluateViewCellReuseID forIndexPath:indexPath];
    cell.commentModel = self.commentArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)setIsMineInfo:(BOOL)isMineInfo {
    _isMineInfo = isMineInfo;
    self.consultView.hidden = isMineInfo;
}

- (void)setCommentArr:(NSArray<SISSCommentModel *> *)commentArr {
    _commentArr = commentArr;
    self.tableViewHeightConstraint.constant = commentArr.count > 2 ? 360 : commentArr.count * 160;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offset = scrollView.contentOffset.y;
    //    NSLog(@"%@",@(offset));
    CGRect rect = self.topBgImageView.frame;
    
    if (offset <= 0) {
        rect.origin.y = offset;
        rect.size.height = 210 - offset;
        self.topBgImageView.frame = rect;
    }
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];

    self.businessContactViewHeightConstraint.constant = self.isMineInfo ? 0.001 : 150;
    self.consultView.hidden = self.isMineInfo;
    self.tableViewHeightConstraint.constant = 0;
    self.tableView.estimatedRowHeight = 160;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSOrderEvaluateViewCell" bundle:nil] forCellReuseIdentifier:orderEvaluateViewCellReuseID];
}


- (IBAction)commentResultButtonClickAction:(UIButton *)sender {
    
    self.selectedButton.selected = NO;
    self.selectedButton = sender;
    self.selectedButton.selected = YES;
    if (sender.tag == 100) {
        
        [self requestTakeLookCommentData];
    } else {
        
        [self requsetServiceCommentData];
    }
    
    [self.tableView reloadData];
    
    NSLog(@"评论");
}

/**
 查看全部评论按钮点击事件
 */
- (IBAction)viewAllCommentButtonClickAction:(id)sender {
    
    SISSReceivedCommentViewController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"SISSReceivedCommentViewController"];
    vc.spId = self.spId;
    
    [self.navigationController pushViewController:vc animated:YES];
    
    NSLog(@"查看全部评论");
}

/**
 请求带看评论数据
 */
- (void)requestTakeLookCommentData {
    
    WeakSelf
    [kNetworkManager requestAgentReceivedTakeLookCommentInfoWithPageNo:1  spId:self.spId  success:^(SISSCommentBaseModel *commentBaseModel) {
        
        self.commentArr = commentBaseModel.data;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
        }];

    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

/**
 请求服务评价数据
 */
- (void)requsetServiceCommentData {
    
    WeakSelf
    [kNetworkManager requestAgentReceivedServiceCommentInfoWithPageNo:1 spId:self.spId success:^(SISSCommentBaseModel *commentBaseModel) {
        
        self.commentArr = commentBaseModel.data;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
        }];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - 咨询相关点击事件
// 电话咨询
- (IBAction)telephoneConsultButtonClickAction:(id)sender {
    
    NSLog(@"电话咨询");
    UIWebView * callWebview = [[UIWebView alloc]init];
//    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",self.houseProgressModel.phoneNumber]]]];
    [[UIApplication sharedApplication].keyWindow addSubview:callWebview];
}

// 在线咨询
- (IBAction)onlineConsultButtonClickAction:(id)sender {
    NSLog(@"在线咨询");
}



@end
